/*
 * @lc app=leetcode.cn id=969 lang=javascript
 *
 * [969] 煎饼排序
 */

// @lc code=start
/**
 * @param {number[]} arr
 * @return {number[]}
 */
function reverse(arr, k) {
    for (let i = 0, j = k - 1; i < j; i++, j--) {
        const tem = arr[i];
        arr[i] = arr[j];
        arr[j] = tem;
    }
}
var pancakeSort = function(arr) {
   if (!arr.length) return [];
   let res = [];
   const sortArr = [...arr].sort((a,b) => a-b);
   for(let i = arr.length - 1; i > 0; i--) {
       const index = arr.indexOf(sortArr[i]);
       res.push(index + 1, i + 1);
       reverse(arr, index + 1);
       reverse(arr, i + 1);
   }
   return res;
};
// @lc code=end

